package com.flight.trans.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.bean.EnterPortWarehousingDetailBean.EnterPortWarehousingData.InwardStorageData;
import com.flight.trans.base.BaseRecyclerAdapter;
import com.flight.trans.base.BaseViewHolder;
import com.flight.trans.global.Constant;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.LogUtils;

import java.util.List;

/**
 * 进港入库
 */
public class EnterPortWarehousingDetailsAdapter extends BaseRecyclerAdapter<InwardStorageData> {

    public EnterPortWarehousingDetailsAdapter(Context mContext, List<InwardStorageData> mList , boolean IsEditor) {
        super(mContext, mList);
        this.IsEditor = IsEditor;
    }

    @Override
    public int setContentView() {
        return R.layout.adapter_enter_port_warehousing_details;
    }

    @Override
    protected void bindData(BaseViewHolder holder, InwardStorageData data, int position) {
        LinearLayout mLlContainerItem = holder.findViewById(R.id.mLlContainerItem);
        TextView mTvOrderNo = holder.findViewById(R.id.mTvOrderNo);
        TextView mTvDestination = holder.findViewById(R.id.mTvDestination);
        TextView mTvProductName = holder.findViewById(R.id.mTvProductName);
        TextView mTvPicking = holder.findViewById(R.id.mTvPicking);
        TextView mTvCount = holder.findViewById(R.id.mTvCount);
        TextView mTvRealCount = holder.findViewById(R.id.mTvRealCount);
        TextView mTvReceiver = holder.findViewById(R.id.mTvReceiver);
        RelativeLayout mRlOperation = holder.findViewById(R.id.mRlOperation);
        TextView mTvStorageArea = holder.findViewById(R.id.mTvStorageArea);
        TextView mTvUnusualStatus = holder.findViewById(R.id.mTvUnusualStatus);
        Button mBtnOperation = holder.findViewById(R.id.mBtnOperation);
        TextView mTvCreateTime = holder.findViewById(R.id.mTvCreateTime);

        boolean beWarehousing = data.getState() == Constant.Be_Warehousing ;

        mTvOrderNo.setBackground(data.getTransferStatus() == Constant.Transport_Code ? mContext.getDrawable(R.mipmap.icon_transport) : null);

        // 设置文字
        mTvOrderNo.setText(data.getOrderNo());
        mTvDestination.setText(data.getOrap());
        mTvProductName.setText(data.getTradeName());
        mTvPicking.setText(data.getPackName());
        mTvRealCount.setText(String.valueOf(data.getRealNum()));
        mTvCount.setText(String.valueOf(data.getNum() != 0 ? data.getNum() : ""));
        mTvReceiver.setText(data.getReceiverName());

        mTvStorageArea.setText((data.getInwardStorageDetailData() != null && data.getInwardStorageDetailData().size() != 0) ? "已选" + data.getInwardStorageDetailData().size() + "项" : "未选区域");
        mTvUnusualStatus.setText(data.getAnomalyCnt() != 0 ? String.valueOf(data.getAnomalyCnt()) : "无");
        mTvCreateTime.setText(CommonUtils.getTimeNorm(data.getOrderCreateTime()));

        if(data.isSelect()) {
            setTextColor(mTvOrderNo, R.color.color_blue, data.isSelect());
            setTextColor(mTvDestination, R.color.color_blue, data.isSelect());
            setTextColor(mTvProductName, R.color.color_blue, data.isSelect());
            setTextColor(mTvPicking, R.color.color_blue, data.isSelect());
            setTextColor(mTvCount, R.color.color_blue, data.isSelect());
            setTextColor(mTvRealCount, R.color.color_blue, data.isSelect());
            setTextColor(mTvCreateTime, R.color.color_blue, data.isSelect());
            setTextColor(mTvStorageArea, R.color.color_blue, data.isSelect());
            setTextColor(mTvUnusualStatus, R.color.color_blue, data.isSelect());
            setTextColor(mTvReceiver, R.color.color_blue, data.isSelect());
        }else {
            setTextColor(mTvOrderNo, R.color.color_red, !beWarehousing);
            setTextColor(mTvDestination, R.color.color_red, !beWarehousing);
            setTextColor(mTvProductName, R.color.color_red, !beWarehousing);
            setTextColor(mTvPicking, R.color.color_red, !beWarehousing);
            setTextColor(mTvCount, R.color.color_red, !beWarehousing);
            setTextColor(mTvRealCount, R.color.color_red, !beWarehousing);
            setTextColor(mTvCreateTime, R.color.color_red, !beWarehousing);
            setTextColor(mTvStorageArea, R.color.color_red, !beWarehousing);
            setTextColor(mTvUnusualStatus, R.color.color_red, !beWarehousing);
            setTextColor(mTvReceiver, R.color.color_red, !beWarehousing);
        }

        // 空白记录
        if(data.isBlankRecord()){
            mLlContainerItem.setBackgroundColor(mContext.getColor(R.color.color_cyan_trans));
            mTvOrderNo.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            mTvDestination.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            setOnTextClickListener(mTvOrderNo , position);
            setOnTextClickListener(mTvDestination , position);
        }

        mBtnOperation.setText(mContext.getString(data.isSelect() ? R.string.text_cancel : R.string.text_confirm));
        mBtnOperation.setBackground(mContext.getDrawable(data.isSelect() ? R.drawable.selector_shape_red : R.drawable.selector_button));
        mRlOperation.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

        if(data.isSelect()){
            mTvOrderNo.setBackgroundColor(mContext.getColor(android.R.color.transparent));
            mTvUnusualStatus.setBackgroundColor(mContext.getColor(android.R.color.transparent));
            mBtnOperation.setVisibility(View.VISIBLE);
        }else {
            if(beWarehousing){
                mBtnOperation.setVisibility(View.VISIBLE);
            }else {
                mBtnOperation.setVisibility(View.GONE);
            }
        }

        if(IsEditor) {
            mTvRealCount.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            setOnTextClickListener(mTvRealCount, position);

            mTvUnusualStatus.setBackground(mContext.getDrawable(R.drawable.selector_line_trans_shape));
            setOnTextClickListener(mTvUnusualStatus, position);

            setOnTextClickListener(mBtnOperation, position);
            setOnTextClickListener(mTvStorageArea, position);

            setItemClickListener(mLlContainerItem, position);
        }
    }

    private void setTextColor(TextView textView , int color ,boolean isSelected){
        textView.setTextColor(mContext.getColor(isSelected ? color : R.color.text_color));
    }
}
